package com.yanqu.road.dao.impl.activity.suitang.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.suitang.data.SuiTangUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class SuiTangUserDataDaoImpl extends TempDataObjectDao<SuiTangUserData> {

    public static String getSuiTangUserDataSql = "select * from `t_u_activity_suitang_data` where `activityId`=? and `userId`=?";

    public SuiTangUserData getSuiTangUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSuiTangUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateSuiTangUserDataSql = "update `t_u_activity_suitang_data` set worshipTime=?,worshipEndPeriodTime=?,positionId=?,zhanGong=?,unionId=?,moveSpeedUpNum=?,skinExchangeMap=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(SuiTangUserData suiTangUserData) {
        suiTangUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, suiTangUserData.getWorshipTime()));
        para.put(i++, new DbParameter(Types.BIGINT, suiTangUserData.getWorshipEndPeriodTime()));
        para.put(i++, new DbParameter(Types.INTEGER, suiTangUserData.getPositionId()));
        para.put(i++, new DbParameter(Types.BIGINT, suiTangUserData.getZhanGong()));
        para.put(i++, new DbParameter(Types.VARCHAR, suiTangUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, suiTangUserData.getMoveSpeedUpNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suiTangUserData.getSkinExchangeMap())));
        para.put(i++, new DbParameter(Types.INTEGER, suiTangUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, suiTangUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSuiTangUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSuiTangUserDataSql = "insert into `t_u_activity_suitang_data`(activityId,userId,worshipTime,worshipEndPeriodTime,positionId,zhanGong,unionId,moveSpeedUpNum,skinExchangeMap) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SuiTangUserData suiTangUserData) {
        suiTangUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, suiTangUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, suiTangUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, suiTangUserData.getWorshipTime()));
        para.put(i++, new DbParameter(Types.BIGINT, suiTangUserData.getWorshipEndPeriodTime()));
        para.put(i++, new DbParameter(Types.INTEGER, suiTangUserData.getPositionId()));
        para.put(i++, new DbParameter(Types.BIGINT, suiTangUserData.getZhanGong()));
        para.put(i++, new DbParameter(Types.VARCHAR, suiTangUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, suiTangUserData.getMoveSpeedUpNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suiTangUserData.getSkinExchangeMap())));
        if (getDb().executeNoneQuery(insertSuiTangUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SuiTangUserData parseData(ResultSet rs) throws SQLException {
        SuiTangUserData suiTangUserData = new SuiTangUserData();
        suiTangUserData.setActivityId(rs.getInt("activityId"));
        suiTangUserData.setUserId(rs.getLong("userId"));
        suiTangUserData.setWorshipTime(rs.getLong("worshipTime"));
        suiTangUserData.setWorshipEndPeriodTime(rs.getLong("worshipEndPeriodTime"));
        suiTangUserData.setPositionId(rs.getInt("positionId"));
        suiTangUserData.setZhanGong(rs.getLong("zhanGong"));
        suiTangUserData.setUnionId(rs.getString("unionId"));
        suiTangUserData.setMoveSpeedUpNum(rs.getLong("moveSpeedUpNum"));
        suiTangUserData.setSkinExchangeMap(JSON.parseObject(rs.getString("skinExchangeMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));

        return suiTangUserData;
    }

}
